// subPages/health/consultHistory/consultHistory.js
const app = getApp()
var util =  require('../../../utils/util.js');
// type  1 咨询历史
/**
 * type
 * 1 咨询历史
 * 2 用户咨询记录
 * 3 专家咨询记录
 */
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downImg: '/resources/theme/down.png',
    upImg: '/resources/theme/up.png',
    date: '',
    isDate: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.type && options.item){
      let infoList = JSON.parse(options.item)
      that.setData({
        type : options.type,
        infoList: infoList,
        dateImgSrc: that.data.downImg,
        isType: true
      })
      // console.log(infoList)
      that.setDate()
      that.setBtn(that.data.type)
      that.setRead()
    }
  },

  //设置按钮
  setBtn:function(type){
    let btnTxt = ''
    let isBtn = false
    switch (type) {
      case '2':
        btnTxt = '继续咨询'
        isBtn = true
        break;
      case '3':
        btnTxt = '进行回复'
        isBtn = true
        break;
    }
    this.setData({
      isBtn: isBtn,
      btnTxt: btnTxt
    })
  },

  //设置日期
  setDate:function(){
    let dateStart = util.setTime(new Date())
    let Y = dateStart.split('-')
    this.setData({
      dateStart: Y[0]- 3,
      dateEnd: dateStart,
    })
  },

  bindDateChange: function(e) {
    this.setData({
      isDate: true,
      date: e.detail.value,
      dateImgSrc: this.data.downImg
    })
    this.getData()
  },

  bindDateCancel:function(){
    this.setData({
      dateImgSrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e){
    var that = this
    if(that.data.dateImgSrc == that.data.downImg){
      that.setData({
        dateImgSrc: that.data.upImg
      })
    }
  },

  //删除标签
  delete:function(){
    this.setData({
      isDate: false,
      date: ''
    })
    this.getData()
  },

  //获取数据
  getData:function(){
    var that = this
    var start_time = ''
    var end_time = ''
    if(that.data.date){
      start_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 00:00:00').getTime())/1000
      end_time = (new Date(String(that.data.date).replace(/-/g,"/") + ' 23:59:59').getTime())/1000
    }
    util.getAjax(app.globalData.apiUrl +'/get_communtiy_health_expert_tall_content',function(res){
      // console.log(res)
      if(res.data.one){
        let list = []
        list.push(res.data.one)
        if(res.data.two.length != 0){
          list = list.concat(res.data.two)
        }
        // console.log(list)
        for(var i in list){
          if(list[i].img.length != 0){
            for(var j in list[i].img){
              list[i].img[j] = app.globalData.apiUrl + list[i].img[j]
            }
          }
        }
        that.setData({
          consultList:list
        })
      }
    },{expert_tall_id: this.data.infoList.id,start_time: start_time,end_time: end_time})
  },

  //设置已读
  setRead:function(){
    var type = ''
    switch (this.data.type) {
      case '2':
        type = 1
        break;
      case '3':
        type = 2
        break;
    }
    util.getAjax(app.globalData.apiUrl +'/save_communtiy_health_expert_tall_status',function(res){
      // console.log(res)
      if(res.data == 1){
        //成功
        if(type == 2){
          //专家端
          app.globalData.identity.num = Number(app.globalData.identity.num) -1;
        }
      }
    },{expert_tall_id: this.data.infoList.id,type: type})
  },

  //咨询回复按钮
  consultBtn:function(){
    var that = this
    wx.navigateTo({
      url: '../consult/consult?type=' + that.data.type + '&item=' + JSON.stringify(that.data.infoList)
    })
  },

  //点击图片
  imgBtn:function(e){
    var list = e.currentTarget.dataset.list;
    var index = e.currentTarget.dataset.idx;
    wx.previewImage({
      urls: list,
      current: list[index]
    })
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isType){
      this.getData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})